﻿#include "../app.h"
#include "../menu_bar/menu_bar.h"
#include "../app_config.h"
#include "app_startup.h"
#include "app_startup_model.h"
#include "app_startup_view.h"
#include "../home/app_home.h"

void on_timer(lv_timer_t* timer) {
    // 进入首页
    app_anim_set_global(LOAD_ANIM_FADE_ON, LV_ANIM_TIME_DEFAULT, lv_anim_path_linear);
    app_switch_by_name(PAGENAME_app_home);
}

/**
 * @brief 事件处理
 * @param event 
*/
static void on_event(lv_event_t* event) {
    // 事件对象
    lv_obj_t* obj = lv_event_get_target(event);
    // 事件类型
    lv_event_code_t code = lv_event_get_code(event);

    // 根节点数据比较特殊，必须通过页面描述
    if (obj == app_startup_page.root) {

    }
    // 其他元素通过对应的view获取
    //else if (obj == app_startup_view.back) {
    //    if (code == LV_EVENT_CLICKED) {
    //        app_switch_by_name("system");
    //    }
    //}
}

/**
 * @brief 事件订阅封装
 * @param obj 
 * @param content 
*/
static void attach_event(lv_obj_t* obj, void* content) {
    lv_obj_set_user_data(obj, content);
    lv_obj_add_event_cb(obj, on_event, LV_EVENT_ALL, content);
}

/**
 * @brief 主要用于加载/初始化数据和界面
 * @param content 上下文
*/
static void app_load(void* content) {
    app_page_t* page = (app_page_t*)content;

    // 1. 数据初始化
    app_startup_model_get_data();

    // 2. UI初始化
    app_startup_view_create(page->root);

    // 3. 事件初始化
    attach_event(page->root, page);
    //attach_event(app_startup_view.back, page);

    lv_timer_t* timer = lv_timer_create(on_timer, 1800, page);
    lv_timer_set_repeat_count(timer, 1);
}

/**
 * @brief 界面加载结束
 * @param content 
*/
static void app_did_load(void* content) {
    app_page_t* page = (app_page_t*)content;

    //lv_obj_fade_out(page->root, 500, 2000);
}

/**
 * @brief 即将显示页面内容
 * @param content
*/
static void app_will_appear(void* content) {
    app_page_t* page = (app_page_t*)content;

    lv_anim_timeline_start(app_startup_view.anim_timeline);
}

/**
 * @brief 显示结束
 * @param content 
*/
static void app_did_appear(void* content) {

}

/**
 * @brief 即将关闭页面展示
 * @param content 
*/
static void app_will_disappear(void* content) {
    app_page_t* page = (app_page_t*)content;
    //lv_obj_fade_out(page->root, 500, 0);
}

/**
 * @brief 页面隐藏
 * @param content 
*/
static void app_did_disappear(void* content) {
    menu_bar_appear(true);
}

/**
 * @brief 页面销毁
 * @param content 
*/
static void app_did_unload(void* content) {
    app_page_t* page = (app_page_t*)content;
    // 1. 移除界面，APP中已经在切换界面时，自动移除了该root下的所有界面
    app_startup_view_delete();
    // 2. 移除数据，如果有
}

static app_ops_t app_ops =
{
    .on_load = app_load,
    .on_did_load = app_did_load,
    .on_will_appear = app_will_appear,
    .on_did_appear = app_did_appear,
    .on_will_disappear = app_will_disappear,
    .on_did_disappear = app_did_disappear,
    .on_did_unload = app_did_unload
};

app_page_t app_startup_page =
{
    .root = NULL,
    .user_data = NULL,
    .anim.is_busy = false,
    .anim.is_enter = false,
    .id = 0,
    .name = PAGENAME_app_startup,
    .ops = &app_ops,
    .state = PAGE_STATE_IDLE
};
